package com.prs.crm.action.stock.purchase;

import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.stock.PurchaseType;
import com.prs.crm.service.stock.PurchaseTypeService;
import com.prs.crm.service.sys.LogService;

public class PurchaseTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PurchaseTypeService purchaseTypeService;

	private PurchaseType type;

	private PaginationSupport pagination;
	
	private LogService logService;
	
	private String typeName;

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	private Integer[] typeIds;

	public void setPurchaseTypeService(PurchaseTypeService purchaseTypeService) {
		this.purchaseTypeService = purchaseTypeService;
	}

	public PurchaseTypeService getPurchaseTypeService() {
		return purchaseTypeService;
	}

	public void setType(PurchaseType type) {
		this.type = type;
	}

	public PurchaseType getType() {
		return type;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	@Action("listPurchaseType")
	public String listPurchaseType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PurchaseType.class);
		this.setPagination(this.getPurchaseTypeService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editPurchaseType")
	public String editPurchaseType() {
		return SUCCESS;
	}

	@Action(value = "savePurchaseType", results = {
			@Result(name = "success", type = "redirect", location = "listPurchaseType"),
			@Result(name = "error", type = "chain", location = "editPurchaseType") })
	public String savePurchaseType() {
		
		
		if (type.getName() == null || type.getName().equals("")) {
			//type = null;
			this.addActionError("名称不能为空");
			return ERROR;
		} 
		if(this.logService.getSearchName(PurchaseType.class, type.getName())){
			if(!(typeName.equals(type.getName()))){
				this.addActionError("名称已存在");
				return ERROR;
			}
		}
			this.getPurchaseTypeService().save(type);
			return SUCCESS;
		
	}

	@Action(value = "removePurchaseType", results = { @Result(name = "success", type = "redirect", location = "listPurchaseType") })
	public String removePurchaseType() {
		this.getPurchaseTypeService().batchRemove(typeIds);
		return SUCCESS;
	}
}
